{include file='header'/}
<body>
<div class="weadmin-nav">
    <span class="layui-breadcrumb">
        <a href="">首页</a><span lay-separator="">/</span>
        <a href="">模块管理</a><span lay-separator="">/</span>
        <a><cite>友情链接管理</cite></a>
    </span>
    <a class="layui-btn layui-btn-sm" style="float:right;margin-top: 5px" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon">&#xe9aa;</i></a>
</div>
<div class="weadmin-body">
    <div class="weadmin-block demoTable">
        <button class="layui-btn" onclick="WeAdminShow('添加友情链接','{:url('add')}','800')"><i class="layui-icon"></i>添加友情链接</button>
        <div class="layui-inline" style="margin-right:15px;">
            <a class="layui-btn layui-btn-danger" data-type="dels"><i class="iconfont icon-delete"></i> 批量删除</a>
        </div>
        <span class="fr" style="line-height:40px;padding-right: 10px;">共有数据：<span  id="total"></span> 条</span>
    </div>
    <script type="text/html" id="status">
        {{#  if(d.status =='1'){ }}
        <span class="layui-badge layui-bg-green"> 已启用 </span>
        {{#  } else { }}
        <span class="layui-badge layui-bg-danger"> 已禁用 </span>
        {{#  } }}
    </script>
    <script type="text/html" id="linktype">
        {{#  if(d.linktype =='0'){ }}
        <span class="layui-badge layui-bg-blue"> 文字链接 </span>
        {{#  } else { }}
        <span class="layui-badge layui-bg-cyan"> 图片链接 </span>
        {{#  } }}
    </script>
    <script type="text/html" id="logo">
        {{#  if(d.logo !=''){ }}
        <img src="{{d.logo}}" onmouseover="hui_img_preview('logo2','{{d.logo}}')" onmouseout="layer.closeAll();" id="logo2" width="40" >
        {{#  } }}
</script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-sm layui-btn-success" onclick="check('{{d.id}}')">检测</a>
        <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    </script>
    <table id="role_list" lay-filter="role_list"></table>
</div>

{include file='footer'/}
<script>
    layui.use(['table'], function(){
        var table = layui.table;
        var tableIns = table.render({
            elem: '#role_list'
            ,page: false
            ,where:{do:1}
            ,method: 'post'
            ,url: '{:url('index')}' //数据接口
            ,done: function(res){ //res 即为原始返回的数据
                $("#total").html(res.count);
            }
            ,cols: [[ //表头
                {type:'checkbox',LAY_CHECKED:false}
                ,{field:'listorder', title:'排序', width:85, align:'center',edit: 'text'}
                ,{field: 'name', title: '网站名称'}
                ,{field: 'url', title: '网站url'}
                ,{field: 'logo', title: '网站logo',width:150,templet: '#logo',align:'center'}
                ,{field: 'username', title: '站长姓名',width:100}
                ,{field: 'linktype', title: '链接类型',width:100,templet: '#linktype'}
                ,{field: 'status', title: '状态',templet: '#status',align:'center',width:90}
                ,{field: 'addtime', title: '添加时间',width:180,align:'center'}
                ,{field: 'link_status', title: '友链状态',align:'center',width:90}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center',width:200}
            ]]
            ,id: 'testReload'
            ,page:true
            ,limit:10
        });
        var $ = layui.$, active = {
            reload: function() {
                //执行重载
                table.reload();
            },
            dels: function(){ //批量删除
                var ids = table_get_ids(table);
                if(ids===false){
                    return false;
                }
                layer.confirm('你确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    var postdata ={ids :ids};
                    posttips("{:url('delete')}",postdata);
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听行工具事件
        table.on('tool(role_list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('你确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    var postdata ={ids :data.id};
                    posttips("{:url('delete')}",postdata);
                });
            } else if(obj.event === 'edit'){
                WeAdminShow("编辑","{:url('edit')}?id="+data.id,'850');
            }
        });

        //监听单元格编辑
        table.on('edit(role_list)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            $.post("{:url('listorder_edit')}",{
                id:data.id,
                value:value
            },function (res) {
                layer.msg(res.msg);
                tableIns.reload();
            })
        });

    });

    function table_get_ids(table) {
        var checkStatus = table.checkStatus('testReload')
            ,data = checkStatus.data;
        if(data.length<1){
            layer.msg('你没有选择任何数据');
            return false;
        }else{
            var ids=[];
            for(var i=0;i<data.length;i++){
                ids +=data[i].id+',';
            }
            ids =ids.slice(0, -1);
            var arr =ids.split(',');
            for(var i in arr){
                (arr[i])
            }
            return arr;
        }
    }
    function check(id) {
        $.post("{:url('check')}",{
            id:id
        },function (res) {
            layer.msg(res.msg);
        })
    }
</script>
</body>
</html>